package com.example.leo.mycharmingstudent.project.maintabfragment.subpagefragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.leo.mycharmingstudent.R;
import com.example.leo.mycharmingstudent.a0917.practice.HandlerUtil;
import com.example.leo.mycharmingstudent.project.constant.ApiInterface;
import com.example.leo.mycharmingstudent.project.entity.KnowledgeTreeEntity;
import com.example.leo.mycharmingstudent.project.maintabfragment.subpagefragment.adapter.SubPageListAdapter;
import com.example.leo.mycharmingstudent.project.maintabfragment.subpagefragment.entity.KnowledgeSubPageEntity;
import com.example.leo.mycharmingstudent.project.util.OkHttpUtil;
import com.google.gson.Gson;

import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

/**
 * Created by Leo on 2020/9/30.
 */

public class SubPageListFragment extends Fragment {

    public static final String CHILDREN_BEAN = "children_bean";

    private Context mContext;
    private List<KnowledgeSubPageEntity.DataBean.DatasBean> mDataList = new ArrayList<>();

    //刷新控件
    private SwipeRefreshLayout srlFragmentSubPage;
    //知识体系二级列表控件
    private RecyclerView rvFragmentSubPage;

    private KnowledgeTreeEntity.DataBean.ChildrenBean mChildrenBean;
    private SubPageListAdapter mSubPageListAdapter;

    public static SubPageListFragment newInstance(KnowledgeTreeEntity.DataBean.ChildrenBean childrenBean) {
        SubPageListFragment fragment = new SubPageListFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable(CHILDREN_BEAN, childrenBean);
        fragment.setArguments(bundle);
        return fragment;
    }

    /**
     * 创建视图View
     * @param inflater
     * @param container
     * @param savedInstanceState
     * @return
     */
    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View inflate = inflater.inflate(R.layout.fragment_sub_page_list, container, false);
        srlFragmentSubPage = inflate.findViewById(R.id.srl_fragment_sub_page_list);
        rvFragmentSubPage = inflate.findViewById(R.id.rv_fragment_sub_page_list);
        return inflate;
    }

    /**
     * Fragment正式依附于Activity
     * 这个生命周期方法之后，获取到的上下文菜单不为null
     * @param savedInstanceState
     */
    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
        initEvent();
    }

    private void initData() {

        mContext = getContext();

        Bundle arguments = getArguments();
        if (arguments != null) {
            mChildrenBean = (KnowledgeTreeEntity.DataBean.ChildrenBean) arguments.getSerializable(CHILDREN_BEAN);
        } else {
            throw new RuntimeException("Please check indicator list!");
        }

        mSubPageListAdapter = new SubPageListAdapter(mContext, mDataList);
        rvFragmentSubPage.setLayoutManager(new LinearLayoutManager(mContext));
        rvFragmentSubPage.setAdapter(mSubPageListAdapter);

        requestSubPageList();

    }

    /**
     * 请求二级列表接口
     */
    private void requestSubPageList() {
        String url = String.format(Locale.CHINA,
                ApiInterface.KNOWLEDGE_FEED_URL + "?cid=%d", 0, mChildrenBean.getId());
        OkHttpUtil.enqueue(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String result = response.body().string();
                final KnowledgeSubPageEntity knowledgeSubPageEntity = new Gson().fromJson(result, KnowledgeSubPageEntity.class);
                HandlerUtil.post(new Runnable() {
                    @Override
                    public void run() {
                        if (knowledgeSubPageEntity.getErrorCode() == 0) {
                            List<KnowledgeSubPageEntity.DataBean.DatasBean> datas = knowledgeSubPageEntity.getData().getDatas();
                            mDataList.clear();
                            mDataList.addAll(datas);
                            mSubPageListAdapter.notifyDataSetChanged();
                        }
                    }
                });

            }
        });
    }

    private void initEvent() {

    }

    public void scrollToTop(){
        rvFragmentSubPage.scrollToPosition(0);
    }
}
